// pages/resu/re su.js
var util = require("../../utils/util.js")
var network = require("../../api/api.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    result: {},
    dialog: false,
    already_number: '',
    total_number: '',
    ckValue: '1',
    btnClass: '',
    btnMargin: '',
    profile: {
      phone: '',
      address: ''
    }
  },
  goIndex: function () {
    if (this.data.ckValue == 1 && this.data.result.test_type == 2 && this.data.result.test_status == 1) {
      this.setData({
        dialog: true
      })
    } else {
      const result = this.data.result
      if (result.test_type === 3 && result.after_test_status) {
          wx.reLaunch({
              url: '../Success/Success?paper_id=' + String(result.paper_id) + '&test_status=2'
          })
      } else {
          wx.reLaunch({
              url: '../index/index'
          })
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    const result = JSON.parse(options.result)
    let userClass = ''
    let btnClass = ''
    let btnMargin = ''
    if (result.test_type != 3 && result.test_status == 1) {
      userClass = 'r_success'
    }
    if (result.test_type != 3 && result.test_status == 0) {
      userClass = 'r_fail'
      btnClass = 'fail-btn'
    }
    if (result.test_type == 3) {
      btnMargin = 'btn-margin'
    }
    var userLogin = wx.getStorageSync('user_login') || {}
    this.setData({
      result: result,
      userClass: userClass,
      btnClass: btnClass,
      btnMargin: btnMargin,
      user_id: String(userLogin.user_id)
    })
    util.get_paper_info(this.data.user_id, options.paper_id, options.user_paper_id, (result) => {
      if (result) {
        that.setData({
          already_number: result.done_questions_num - 0,
          total_number: result.questions_num - 0
        })
      }
    })
  },
  radioChange: function(e) {
    this.setData({
      ckValue: e.detail.value
    })
  },
  profileInput: function(e) {
    const key = e.target.dataset.key
    const data = new Object()
    data[key] = e.detail.value
    this.setData({
      profile: Object.assign({}, this.data.profile, data)
    })
  },
  apply_ertificate: function(e) {
    if (!this.data.profile.phone) {
      wx.showToast({
        title: '请输入收货人电话',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (!this.data.profile.address) {
      wx.showToast({
        title: '请输入邮寄地址',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    const that = this
    var params = new Object()
    params.business_param = {}
    params.business_param.user_id = this.data.user_id;
    params.business_param.phone = this.data.profile.phone;
    params.business_param.address = this.data.profile.address;
    network.POST({
      params: params,
      url: "apply_ertificate",
      success: function (res) {
        wx.reLaunch({
          url: '../index/index'
        })
        //拿到解密后的数据，进行代码逻辑
      },
      fail: function () {
        //失败后的逻辑

      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})